#pragma once

#include "ListItem.h"
#include "ListDetails.h"
#include "PoomPictures.h"
#include "CommonWM.h"

#define LIST_SEPARATOR_HEIGHT 1

enum OptDrawItem
{
  odiBackground,
  odiGroupHeader,
  odiPrimaryText,
  odiDialingNo,
  odiPicture,
  odiSecondaryText,
  odiCount
};

enum ContactItemType
{
  citNone    = 0,
  citPoom    = 1,
  citCallLog = 2,
  citSim     = 3,
  citNewContact = 4
};

/// List DATA
class CListItemBase : public CListItem
{
private:
  TCHAR    szDialingNumber[PRIMARY_TEXT_LENGTH];		
  TCHAR    szDialingNumberDisplay[PRIMARY_TEXT_LENGTH];

public:
  CEOID    oId;
  TCHAR    szPrimaryText[PRIMARY_TEXT_LENGTH];
  TCHAR    wcGroup;
  int      nPrimaryTextLength;
  int      nDialingNumberLength;
  int      nDialingNumberLengthDisplay;

  PoomPicture* picture;

  CListItemBase();

  TCHAR *GetDialingNumber()
  { return szDialingNumber; }

  virtual void Clear();
  virtual ContactItemType GetType() const
  { return citNone; }

  void SetPrimaryText(const TCHAR* szText)
  { ::StringCchCopy(szPrimaryText, PRIMARY_TEXT_LENGTH, szText); nPrimaryTextLength = ::_tcslen(szPrimaryText); }
  
  void SetDialingNo(const TCHAR *szText, const TCHAR *suffix)
  { ::StringCchCopy(szDialingNumber, PRIMARY_TEXT_LENGTH, szText); 
    ::StringCchCopy(szDialingNumberDisplay, PRIMARY_TEXT_LENGTH, szText);
    if (suffix[0] != 0)
    {
      ::StringCchCat(szDialingNumberDisplay, PRIMARY_TEXT_LENGTH, _T(" "));
      ::StringCchCat(szDialingNumberDisplay, PRIMARY_TEXT_LENGTH, suffix);
    }
    nDialingNumberLength = ::_tcslen(szDialingNumber); 
    nDialingNumberLengthDisplay = ::_tcslen(szDialingNumberDisplay); 
  }

  // Drawing
  virtual void Draw(HDC hdc, const RECT& rc) const {}

  virtual void OptDrawItemOn(HWND hParent, HDC hdc, const RECT& rItem, OptDrawItem odi) const;
  virtual void OptDrawBackground(HWND hParent, HDC hdc, const RECT& rc) const;
  virtual void OptDrawGroup(HWND hParent, HDC hdc, const RECT& rc) const;
  virtual void OptDrawPrimary(HWND hParent, HDC hdc, const RECT& rc) const;
  virtual void OptDrawDialingNo(HWND hParent, HDC hdc, const RECT& rc) const;
  virtual void OptDrawPicture(HWND hParent, HDC hdc, const RECT& rc) const;

  virtual COLORREF GetPrimaryColor(HWND hParent) const;
  virtual COLORREF GetSecondaryColor(HWND hParent) const; // avian
  virtual COLORREF GetSecondary2Color(HWND hParent) const; // avian

  // Interface
  virtual bool OnMouseClick(HWND hParent, int x, int y);
  virtual bool OnKeyUp(HWND hParent, int vKey); 
  virtual bool OnLongTap(HWND hParent);
  virtual bool OnDefaultAction(HWND hParent);

  virtual bool IsLongTapValid(HWND hParent)
  { return nDialingNumberLength > 0; }

  virtual bool IsCommandValid(MenuCommands eCmd)
  { return false; }
  virtual bool OnCommand(HWND hParent, MenuCommands eCmd)
  { return false; }

  virtual void GetGroup(HWND hParent, TCHAR * pszGroup) const
  { pszGroup[0] = wcGroup; pszGroup[1] = 0; }
  virtual void GetGroupIndicator(HWND hParent, TCHAR * pszGroup) const
  { return GetGroup(hParent, pszGroup); }

  // Actions
  void DoDefaultAction(HWND hParent);

  // Details
  virtual bool GetDetails(HWND hParent, CListDetails& details) = 0;

public:
  CEOID FindOid(HWND hParent, const TCHAR* szName, const TCHAR* szNumber, bool searchInList) const;
  bool GetNameFromNumber(HWND hParent, TCHAR* szOutName, const TCHAR* szNumber, bool searchInList) const;
};

class CallLogEntry;

class CListItemCallLog : public CListItemBase
{
public:
  CallLogEntry* cEntry;
  TCHAR    szSecondaryText[SECONDARY_TEXT_LENGTH];
  TCHAR    szSecondaryText2[SECONDARY_TEXT_LENGTH];
  int      nSecondaryTextLength;
  int      nSecondaryText2Length;

  CListItemCallLog();

  virtual void Clear();

  ContactItemType GetType() const
  { return citCallLog; }

  void SetSecondaryText(const TCHAR* szText)
  { ::StringCchCopy(szSecondaryText, PRIMARY_TEXT_LENGTH, szText); nSecondaryTextLength = ::_tcslen(szSecondaryText); }
  void SetSecondaryText2(const TCHAR* szText)
  { ::StringCchCopy(szSecondaryText2, PRIMARY_TEXT_LENGTH, szText); nSecondaryText2Length = ::_tcslen(szSecondaryText2); }

  // Drawing
  virtual void OptDrawItemOn(HWND hParent, HDC hdc, const RECT& rItem, OptDrawItem odi) const;
  virtual void OptDrawSecondary(HWND hParent, HDC hdc, const RECT& rc) const;

  virtual COLORREF GetPrimaryColor(HWND hParent) const;

  // Interface
  virtual bool IsCommandValid(MenuCommands eCmd);
  virtual bool OnCommand(HWND hParent, MenuCommands eCmd);

  virtual void GetGroup(HWND hParent, TCHAR * pszGroup) const;
  virtual void GetGroupIndicator(HWND hParent, TCHAR * pszGroup) const;

  // Details
  virtual bool GetDetails(HWND hParent, CListDetails& details);

protected:
  void printDate(TCHAR * str, int strLength, FILETIME ft);
  void printDuration(TCHAR * str, int strLength, FILETIME ftStart, FILETIME ftEnd);
};

class CListItemPoom : public CListItemBase
{
public:
  bool     isFavorite;
  CEPROPID dialingNumberProp;

  CListItemPoom();

  virtual void Clear();

  ContactItemType GetType() const
  { return citPoom; }

  // Interface
  virtual bool IsCommandValid(MenuCommands eCmd);
  virtual bool OnCommand(HWND hParent, MenuCommands eCmd);

  virtual COLORREF GetPrimaryColor(HWND hParent) const;

  // Actions
  bool Display(HWND hWnd, bool edit);
  bool ToggleFavorite(HWND hWnd);
  bool SetPicture(HWND hWnd);

  // Details
  virtual bool GetDetails(HWND hParent, CListDetails& details);
};

class CListItemSim : public CListItemBase
{
public:
  CListItemSim()
  {}

  ContactItemType GetType() const
  { return citSim; }

  // Interface
  virtual bool IsCommandValid(MenuCommands eCmd);
  virtual bool OnCommand(HWND hParent, MenuCommands eCmd);

  // Details
  virtual bool GetDetails(HWND hParent, CListDetails& details);
};

class CListItemNewContact : public CListItemBase
{
public:
  CListItemNewContact()
  {}

  ContactItemType GetType() const
  { return citNewContact; }

  // Interface
  virtual bool OnMouseClick(HWND hParent, int x, int y);
  virtual bool OnKeyUp(HWND hParent, int vKey); 
  virtual bool OnLongTap(HWND hParent);
  virtual bool OnDefaultAction(HWND hParent);

  virtual bool IsLongTapValid(HWND hParent)
  { return false; }

  // Details
  virtual bool GetDetails(HWND hParent, CListDetails& details)
  { details.Clear(); return false; }
};